//
//  GetCoinVC.m
//  digitalCurrency
//
//  Created by 默认账户 on 2019/4/3.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "GetCoinVC.h"
#import "HomeNetManager.h"
#import "UIImage+Extension.h"
#import "CTQGCell.h"
#import "CTRechargeRecordModel.h"

@interface GetCoinVC ()
    
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIButton *sectionHeader;

@property (nonatomic, strong) UIImageView *qrcodeView;
@property (nonatomic, strong) UILabel *addrLabel;
@property (nonatomic, strong) UILabel *tipsLabel;

@property (nonatomic, strong) UIButton *copBtn;
    
@property (nonatomic, strong) NSMutableArray *records;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) LYEmptyView *emptyView;
    
@end

@implementation GetCoinVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        _records = [NSMutableArray array];
        _page = 1;
    }
    return self;
}
    
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    self.viewTitle.text = LocalizationKey(@"My Collection Code");
    [self rightItem];

    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[CTQGCell class] forCellReuseIdentifier:@"qgcell"];
    self.tableView.separatorColor  = [UIColor clearColor];
    self.tableView.rowHeight = 70;
    
    [self setHeadView];
    
    [self headRefreshWithScrollerView:self.tableView];
    [self footRefreshWithScrollerView:self.tableView];

    NSDictionary *dic = @{@"pageNum":@(_page),@"pageSize":@"20",@"coinId":self.unit};
    [self getRechargeRecordList:dic];

    _addrLabel.text   = self.address;
    _qrcodeView.image = [UIImage createNonInterpolatedUIImageWithString:self.address withSize:_qrcodeView.size.width];
}

- (void)setHeadView{
    
    self.tableView.tableHeaderView = self.headerView;
    
    UIView *topView = [[UIView alloc]init];
    [self.headerView addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(0);
        make.height.equalTo(146);
    }];
    topView.backgroundColor = tMainColor;
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(16, 20, kWindowW-32, 375)];
    [self.headerView addSubview:bgView];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 6.0;
    bgView.layer.shadowColor = [UIColor colorWithHEX:@"6E6E6E"].CGColor;
    bgView.layer.shadowOffset = CGSizeMake(0, 0);
    bgView.layer.shadowOpacity = 0.21;
    
    [bgView addSubview:self.qrcodeView];
    [bgView addSubview:self.tipsLabel];
    [bgView addSubview:self.copBtn];
    [bgView addSubview:self.addrLabel];
    
    UIView *lineView = [[UIView alloc]init];
    [bgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(0);
        make.bottom.equalTo(-52);
        make.height.equalTo(0.5);
    }];
    lineView.backgroundColor = tTableViewCellLine;
    
//    UILabel *addrTitleLB = [[UILabel alloc]initWithFrame:CGRectMake(15, 342, 60, 15)];
//    [bgView addSubview:addrTitleLB];
//    addrTitleLB.textColor = tThreeColor;
//    addrTitleLB.font = kFont(12);
//    addrTitleLB.text = LocalizationKey(@"我的地址：");
    
    [self.addrLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(15);
        make.right.equalTo(-15);
        make.centerY.equalTo(375/2.0-25);
    }];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}
    
- (void)refreshHeaderAction {
    _page = 1;
    NSDictionary *dic = @{@"pageNum":@(_page),@"pageSize":@"20",@"coinId":self.unit};

    [self getRechargeRecordList:dic];
}
    
- (void)refreshFooterAction {
    _page++;
    NSDictionary *dic = @{@"pageNum":@(_page),@"pageSize":@"20",@"coinId":self.unit};
    [self getRechargeRecordList:dic];
}
    
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.records.count;
}
    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CTQGCell *cell = [CTQGCell createTableView:tableView];
    CTRechargeRecordModel *model = _records[indexPath.row];
    cell.createTime.text = model.rechargeDate;
    cell.coinName.text   = model.fromAddress;
    cell.coinName.font = kFont(12);
    cell.coinNumber.text = [NSString stringWithFormat:@"+%@",[ToolUtil judgeStringForDecimalPlaces:[model.amount stringValue] point:4]];

    return cell;
}
    
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.sectionHeader;
}
    
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}
    
- (void)copaddr {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.addrLabel.text;
    [self.view makeToast:LocalizationKey(@"copyscuuses") duration:1.5 position:CSToastPositionCenter];
}

- (void)getRechargeRecordList:(NSDictionary *)dic {
    [EasyShowLodingView showLodingText:LocalizationKey(@"loading")];
    [HomeNetManager getMemberDepositInfo:dic CompleteHandle:^(id resPonseObj, int code) {
        [EasyShowLodingView hidenLoding];
        if (code) {
            NSInteger codeInt = [resPonseObj[@"code"] integerValue];
            NSArray *data = resPonseObj[@"data"][@"list"];
            if (codeInt == 0) {
                
                if (_page == 1) {
                    [self.records removeAllObjects];
                }
                else {
                    if (data.count == 0) {
                        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
                    }
                    return ;
                }
                for (NSDictionary *sub in data) {
                    CTRechargeRecordModel *model = [CTRechargeRecordModel mj_objectWithKeyValues:sub];
                    [self.records addObject:model];
                }
//                self.tableView.ly_emptyView = self.emptyView;
                [self.tableView reloadData];
            }
            else {
                [self.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }
        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

- (void)rightItem {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,78, 38)];
    imageView.contentMode = UIViewContentModeCenter;
    imageView.image = [UIImage imageNamed:@"logo"];
    //添加到导航条
    UIBarButtonItem *leftBarButtomItem = [[UIBarButtonItem alloc]initWithCustomView:imageView];
    self.navigationItem.rightBarButtonItem = leftBarButtomItem;
}
    
- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 405)];
        _headerView.backgroundColor = [UIColor whiteColor];
    }
    
    return _headerView;
}
    
- (UIButton *)sectionHeader {
    if (!_sectionHeader) {
        _sectionHeader = [UIButton buttonWithType:UIButtonTypeSystem];
        _sectionHeader.backgroundColor = [UIColor whiteColor];
        [_sectionHeader setTitle:LocalizationKey(@"Reharge record") forState:UIControlStateNormal];
        [_sectionHeader setTitleColor:tThreeColor forState:UIControlStateNormal];
        _sectionHeader.titleLabel.font = kFont(14);
        _sectionHeader.frame = CGRectMake(0, 0, SCREEN_WIDTH, 60);
    }
    
    return _sectionHeader;
}

- (UIImageView *)qrcodeView {
    if (!_qrcodeView) {
        _qrcodeView = [[UIImageView alloc] initWithFrame:CGRectMake((kWindowW-32-170)/2.0, 24, 170, 170)];
        _qrcodeView.backgroundColor = [UIColor whiteColor];
    }
    
    return _qrcodeView;
}

- (UILabel *)tipsLabel {
    if (!_tipsLabel) {
        _tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 210, SCREEN_WIDTH-92, 20)];
        _tipsLabel.font = [UIFont systemFontOfSize:15];
        _tipsLabel.textColor = tThreeColor;
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
        _tipsLabel.text = LocalizationKey(@"Scan the qr code to pay me");
    }
    
    return _tipsLabel;
}

- (UILabel *)addrLabel {
    if (!_addrLabel) {
        _addrLabel = [[UILabel alloc] init];
        _addrLabel.font = [UIFont systemFontOfSize:12];
        _addrLabel.textColor = tMainColor;
        _addrLabel.numberOfLines = 0;
        _addrLabel.text = @"";
        _addrLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _addrLabel;
}

- (UIButton *)copBtn {
    if (!_copBtn) {
        _copBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _copBtn.frame = CGRectMake(14,253, SCREEN_WIDTH-60, 44);
        _copBtn.layer.masksToBounds = YES;
        _copBtn.layer.cornerRadius = 4;
        _copBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        _copBtn.backgroundColor = tMainColor;
        [_copBtn setTitle:LocalizationKey(@"Duplicate the Receipt Address") forState:UIControlStateNormal];
        [_copBtn setTitleColor:[UIColor colorWithHEX:@"#F6F6F6"] forState:UIControlStateNormal];
        [_copBtn addTarget:self action:@selector(copaddr) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _copBtn;
}
- (LYEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [LYEmptyView emptyViewWithImageStr:@"emptyData" titleStr:[[ChangeLanguage bundle] localizedStringForKey:@"NoData" value:nil table:@"English"]];
        _emptyView.titleLabTextColor = tThreeColor;
        _emptyView.titleLabFont = [UIFont systemFontOfSize:19];
        _emptyView.contentViewOffset = 200;
    }
    return _emptyView;
}

@end
